package com.yannuo.util;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JdkProxyHandler implements InvocationHandler {
    private final Object target;

    public JdkProxyHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        System.out.println("[JDK代理] 调用方法: " + methodName);
        long startTime = System.currentTimeMillis();

        if ("cancelOrder".equals(methodName)) {
            if (args != null && args.length > 0 && args[0] == null) {
                throw new IllegalArgumentException("订单ID不能为空");
            }
        }

        Object result = method.invoke(target, args);

        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        System.out.println("[JDK代理] 方法耗时: " + duration + "ms");
        return result;
    }
}